//
//  TPHomeItemViewController.m
//  XTCloud
//
//  Created by QA on 2017/8/24.
//  Copyright © 2017年 ShangHai HaoLei Software Technology Co., Ltd. All rights reserved.
//

#import "TPHomeItemViewController.h"

#import "TPHomeViewCell.h"
#import "DateTimeFormat.h"

#import "MJRefresh.h"
#import "CircleProgressView.h"
#import "WebServiceCommonRequest.h"
#import "NSDictionary+DataFormat.h"

#import "UIColorDispose.h"
#import "PMJobOrderDetailViewController.h"
#import "PMJobOrderListViewController.h"

#import "UIView+AssistView.h"

#define TPHomeItemFooterOffice 200

@interface TPHomeItemViewController ()

@property (nonatomic,strong) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSString *state;
@property (strong, nonatomic) NSMutableArray *rwdResultData;

@end

@implementation TPHomeItemViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self setTitle: @"实时生产看板"];
    }
    return self;
}

- (id)initWithState:(NSString*)state
{
    self = [super init];
    if(self){
        self.state = state;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.rwdResultData = [NSMutableArray array];
    
    self.tableView.tableFooterView= [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 10)];
    [self.tableView registerClass:[TPHomeViewCell class] forCellReuseIdentifier:@"TPHomeViewCell"];
    
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    [self.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
    [self updateStationNotification:nil];
}

//更新页面数据
- (void)updateViewData
{
    [self.rwdResultData removeAllObjects];
    [self.tableView reloadData];
    
    //显示进度条
    [self.tableView showViewProgressRect:CGRectMake(0, 0, SuperWidth, TPHomeItemFooterOffice)];
    [self getNetworkBaseData];
}

#pragma mark 开始进入刷新状态
- (void)headerRereshing
{
    [self getNetworkBaseData];
}

- (void)getNetworkBaseData
{
    //站点ID
    StationInfoEntity *model = [[GetUserStationService sharedService] getSelectStationEntity];
    
    //@状态（全部、供毕、正供、待供）
    WebServiceCommonRequest *content = [[WebServiceCommonRequest alloc] init];
    [content setInterfaceType:InterfaceTypeERP];
    [content setModuleType:@"SCDD_HOME_LIST_ALL"];
    [content setResultBackType:ResultBackTypeCommonUnordered];
    [content setStationID:[model.stationId integerValue]];
    [content addParameterKey:@"状态" value:self.state];
    
    [content startAsyncConnectCompleted:^(ServiceResultModel *result, NSDictionary *userInfo) {
        [self.tableView removeAllCustomFromSuperview];
        [self.tableView headerEndRefreshing];
        
        //添加数据，刷新页面
        NSArray *rwdArray = nil;
        if(result.resultArray && result.resultArray.count>0){
            rwdArray = [result.resultArray objectAtIndex:0];
        }
        
        if(rwdArray && rwdArray.count>0){
            [self.rwdResultData removeAllObjects];
            [self.rwdResultData addObjectsFromArray:rwdArray];
            [self.tableView reloadData];
        } else {
            [self.tableView showPromptViewText:@"暂无任务单" rect:CGRectMake(0, 0, SuperWidth, TPHomeItemFooterOffice) blcok:nil];
        }
        
    } failed:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        [self.tableView headerEndRefreshing];
        [self.tableView showPromptViewText:@"哦，出错了" rect:CGRectMake(0, 0, SuperWidth, TPHomeItemFooterOffice) blcok:^{
            [self updateStationNotification:nil];
        }];
        
    } abnormal:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        [self.tableView headerEndRefreshing];
        [self.tableView showPromptViewText:@"无法连接网络" rect:CGRectMake(0, 0, SuperWidth, TPHomeItemFooterOffice) blcok:^{
            [self updateStationNotification:nil];
        }];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma make - Station Notification

-(void)updateStationNotification:(StationInfoEntity*)model
{
    [self.rwdResultData removeAllObjects];
    [self.tableView reloadData];
    
    //显示进度条
    [self.tableView showViewProgressRect:CGRectMake(0, 0, SuperWidth, TPHomeItemFooterOffice)];
    [self getNetworkBaseData];
}

#pragma mark - Table view data source

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

#pragma mark UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.rwdResultData.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TPHomeViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TPHomeViewCell"];
    
    NSDictionary *dictionary = [self.rwdResultData objectAtIndex:indexPath.section];
    cell.RWDCodeLabel.text = [dictionary objectFormatStringForKey:@"任务单编号" defaults:@"未知"];
    cell.GCMCLabel.text = [dictionary objectFormatStringForKey:@"工程名称" defaults:@"未知"];
    //cell.SGDWLabel.text = [dictionary objectFormatStringForKey:@"施工单位" defaults:@"未知"];
    cell.SGBWLabel.text = [dictionary objectFormatStringForKey:@"施工部位" defaults:@"未知"];
    cell.GDDZLabel.text = [dictionary objectFormatStringForKey:@"工地地址" defaults:@"未知"];
    cell.StateLabel.text = [dictionary objectFormatStringForKey:@"状态" defaults:@"未知"];
    cell.CCLabel.text = [dictionary objectFormatStringForKey:@"数量" defaults:@"未知"];
    cell.QDDJLabel.text = [dictionary objectFormatStringForKey:@"砼强度" defaults:@"未知"];
    cell.JZFSLabel.text = [dictionary objectFormatStringForKey:@"浇筑方式" defaults:@"未知"];
    cell.JHKPLabel.text = [dictionary objectFormatStringForKey:@"工地计划开盘时间" defaults:@"未知"];
    
    return cell;
}

#pragma mark UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.0001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 108;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    StationInfoEntity *model = [[GetUserStationService sharedService] getSelectStationEntity];
    NSDictionary *dictionary = [self.rwdResultData objectAtIndex:indexPath.section];
    
    PMJobOrderDetailViewController *viewController = [[PMJobOrderDetailViewController alloc] initWithValue:dictionary];
    [viewController setStationName:model.stationName StationID:[model.stationId integerValue]];
    [self.navigationController pushViewController:viewController animated:YES];
}

@end
